package gold.digger;

import gold.utils.InputUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC658 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public List<Integer> findClosestElements(int[] arr, int k, int x) {
            int left = 0, right = arr.length - k, mid = 0;
            while (left < right) {
                mid = left + (right - left) / 2;
                if (x - arr[mid] <= arr[mid + k] - x) right = mid;
                else left = mid + 1;
            }

            List<Integer> res = new ArrayList<>();
            for (int i : arr) {
                res.add(i);
            }
            return res.subList(left, left + k);
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[1,2,3,4,5]");
        System.out.println(solution.findClosestElements(arr, 4, -1).toString());
    }

    public static void main(String[] args) throws Exception {
        LC658 an = new LC658();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
